package com.xzx.ecommercedw.flume.interceptor;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @author xinzhixuan
 * @version 1.0
 * @date 2024/9/17 18:24
 */
public class LogEventAddTimestampInterceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {
        String body = new String(event.getBody(), StandardCharsets.UTF_8);
        Map<String, String> headers = event.getHeaders();
        headers.put("timestamp", body.split("\\|")[0]);
        return event;
    }

    @Override
    public List<Event> intercept(List<Event> events) {
        events.forEach(this::intercept);
        return events;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder {

        @Override
        public Interceptor build() {
            return new LogEventAddTimestampInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
